# -*- coding: utf-8 -*-
'''
@文件    :administrator.py
@说明    :管理员管理
@时间    :2023/12/19 19:57:04
@作者    :浪晋
@版本    :1.0
'''

from app.admin import auth
from flask import request,g
from utils.requetFunc import RequestParser,pageConvert
from utils.responseFunc import resultMsg
from utils.commomFunc import encry


@auth.route("/administrator/list",methods=["post"])
def systemUserList():
    """
    获取管理员列表
    """
    db = g.db
    body = request.get_json()
    nickName = body.get("nickName")
    phone = body.get("phone")
    deptId = body.get("deptId")
    status = body.get("status")
    pageSize = body.get("pageSize")
    pageNum = body.get("pageNum")
    parse = RequestParser()
    parse.add_argument(value=nickName,name="姓名",remark="")
    parse.add_argument(value=phone,name="电话",remark="")
    parse.add_argument(value=status,name="状态",remark="")
    parse.add_argument(value=deptId,name="部门",remark="")
    parse.add_argument(value=pageSize,name="每页显示的数据数量",required=True,type=(int),remark="必填")
    parse.add_argument(value=pageNum,name="页码",required=True,type=(int),remark="必填")
    parseMsg = parse.parse_args()
    if parseMsg :
        return resultMsg(code=0, msg=parseMsg)
    sql = "select a.id,a.account,a.nickName,a.gender,\
    a.phone,a.avatar,a.remark,a.status,a.deptId,b.department,a.roleId,c.role,\
    a.createTime,a.updateTime,a.lastLoginTime,a.lastLoginIp from tb_system_user  a \
    join tb_system_department b on a.deptId = b.id \
    join tb_system_role c on c.id = a.roleId   \
    where a.status != 0  "
    csql = "select count(0) total from tb_system_user  a \
    join tb_system_department b on a.deptId = b.id \
    join tb_system_role c on c.id = a.roleId \
    where a.status != 0   "
    if nickName:
        sql = sql + " and a.nickName like '%{}%'".format(nickName)
        csql = csql + " and a.nickName like '%{}%'".format(nickName)
    if phone:
        sql = sql + " and a.phone like '%{}%'".format(phone)
        csql = csql + " and a.phone like '%{}%'".format(phone)
    if status:
        sql = sql + " and a.status = {}".format(status)
        csql = csql + " and a.status = {}".format(status)
    if deptId:
        sql = sql + " and a.deptId = {}".format(deptId)
        csql = csql + " and a.deptId = {}".format(deptId)
    sql = sql + " order by createTime desc limit {},{};".format(*pageConvert(pageNum,pageSize))
    result = db.fetchall(sql)
    cresult = db.fetchone(csql)
    return resultMsg(code=1,msg="获取成功",data=result,total=cresult["total"])



@auth.route("/administrator/create",methods=["post"])
def create_adminInfo():
    """
    新增管理员基础信息
    """
    db = g.db
    body = request.get_json()
    avatar = body.get("avatar")
    account = body.get("account")
    nickName = body.get("nickName")
    password = body.get("password")
    phone = body.get("phone")
    deptId = body.get("deptId")
    roleId = body.get("roleId")
    gender = body.get("gender")
    remark = body.get("remark")
    parse = RequestParser()
    parse.add_argument(value=account,name="账号",required=True,regex="^[a-zA-Z0-9]{5,12}$",remark="只能是小写字母")
    parse.add_argument(value=password,name="密码",required=True,regex="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,16}$",remark="大小写字母和数字的组合")
    parse.add_argument(value=avatar,name="头像",required=True,regex="(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*,]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)|([a-zA-Z]+.\w+\.+[a-zA-Z0-9\/_]+)",remark="")
    parse.add_argument(value=nickName,name="花名",required=True,regex="^[\u4e00-\u9fa5]{1,9}$",remark="必填且只能是汉字")
    parse.add_argument(value=phone,name="手机号",required=True,regex="^1[3456789]\d{9}$",remark="必填")
    parse.add_argument(value=deptId,name="部门ID",required=True,remark="必填",type=int)
    parse.add_argument(value=roleId,name="角色ID",required=True,remark="必填",type=int)
    parse.add_argument(value=gender,name="性别",remark="必填",type=int)
    parse.add_argument(value=remark,name="备注",required=True,remark="")
    parseMsg = parse.parse_args()
    if parseMsg :
        return resultMsg(code=0, msg=parseMsg)
    sql = "select id from tb_system_user where account = '{}' or phone = '{}' and status != 0;".format(account,phone)
    result = db.fetchall(sql)
    if result:
        return resultMsg(code=0, msg="该账号或手机号已被注册！")
    sql = "insert into tb_system_user (account,roleId,deptId,password,avatar,nickName,gender,phone,remark) values (%s,%s,%s,%s,%s,%s,%s,%s,%s);"
    db.execute(sql,(account,roleId,deptId,encry(password),avatar,nickName,gender,phone,remark))
    db.commit()
    return resultMsg(msg="新增成功")




@auth.route("/administrator/edit",methods=["post"])
def edit_adminInfo():
    """
    修改管理员基础信息
    """
    db = g.db
    body = request.get_json()
    rowId = body.get("id")
    avatar = body.get("avatar")
    nickName = body.get("nickName")
    phone = body.get("phone")
    deptId = body.get("deptId")
    roleId = body.get("roleId")
    gender = body.get("gender")
    remark = body.get("remark")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="ID",required=True,type=(int),remark="必填")
    parse.add_argument(value=avatar,name="头像",required=True,regex="(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*,]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)|([a-zA-Z]+.\w+\.+[a-zA-Z0-9\/_]+)",remark="")
    parse.add_argument(value=nickName,name="花名",required=True,regex="^[\u4e00-\u9fa5]{1,9}$",remark="必填且只能是汉字")
    parse.add_argument(value=phone,name="手机号",required=True,regex="^1[3456789]\d{9}$",remark="必填")
    parse.add_argument(value=deptId,name="部门ID",required=True,remark="必填",type=int)
    parse.add_argument(value=roleId,name="角色ID",required=True,remark="必填",type=int)
    parse.add_argument(value=gender,name="性别",remark="必填",type=int)
    parse.add_argument(value=remark,name="备注",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_user set avatar = %s,nickName=%s,phone=%s,deptId=%s,roleId=%s,gender=%s,remark=%s where id = %s ;"
    db.execute(sql,(avatar,nickName,phone,deptId,roleId,gender,remark,rowId))
    db.commit()
    return resultMsg(msg="修改成功")



@auth.route("/administrator/state",methods=["post"])
def setAdminStatus():
    """
    修改管理员状态。0删除1正常2冻结
    """
    db = g.db
    body = request.get_json()
    rowId = body.get("id")
    status = body.get("status")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="ID",required=True,regex="^[0-9]*$",remark="必填")
    parse.add_argument(value=status,name="状态",required=True,type=(int),lenght=(1,2),remark="必填")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_user set status = %s where id = %s and status != 0 ;"
    db.execute(sql,(status,rowId))
    db.commit()
    return resultMsg(msg="操作成功")


@auth.route("/administrator/repw",methods=["post"])
def setAdmin_repw():
    """
    重置密码
    """
    db = g.db
    body = request.get_json()
    rowId = body.get("id")
    password = body.get("password")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="管理员ID",required=True,regex="^[0-9]*$",remark="")
    parse.add_argument(value=password,name="密码",required=True,regex="^[0-9]*$",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_user set password = %s where id = %s;"
    db.execute(sql,(encry(password),rowId))
    db.commit()
    return resultMsg(msg="操作成功")


@auth.route("/administrator/del",methods=["post"])
def setAdmin_del():
    """
    删除管理员
    """
    db = g.db
    body = request.get_json()
    rowId = body.get("id")
    parse = RequestParser()
    parse.add_argument(value=rowId,name="管理员ID",required=True,regex="^[0-9]*$",remark="")
    parseMsg = parse.parse_args()
    if parseMsg:
        return resultMsg(code=0, msg=parseMsg)
    sql = "update tb_system_user set status = 0 where id = %s;"
    db.execute(sql,(rowId))
    db.commit()
    return resultMsg(msg="操作成功")

